<?php
/*
 * index.php
 *     Loads a specified page from the Content database. You must specify the
 *     page Alias in the 'page' query parameter, i.e.
 *     "index.php?page=index.html"
 *
 *     You can also pass the 'page' parameter in a POST request.
 *
 *     Invalid content requests return a 404 status page
 *
 */
require_once("includes/config.php");

// Get 'page' parameter from POST or GET
$pageAlias = trim ((!empty($_POST['page'])) ? $_POST['page'] : $_GET['page'] );

$page = NULL;

// If page was not given in request parameters, use default page
if (strlen($pageAlias) === 0) {
    $pageAlias = $defaultPage;
}

// Look up content in database
require_once("admin/includes/dbfunctions.php");
$page = getPageInfo($pageAlias);
// If page was found in database, but file does not exist, return error
if ($page !== NULL) {
    $filename = $contentRoot . $page['Filename'];
    if (!file_exists($filename)) {
        $page = NULL;
    }

    // If page has not been published, return error
    if ($page['Approve'] == 0) {
        $page = NULL;
    }
}


if ($page !== NULL) {
    // If content was found, load content
    $TEMPLATE_PAGE_TITLE = $page['Title'];
    $TEMPLATE_PAGE_CONTENT = file_get_contents($filename);
    include($templateFile);
} else {
    // Return 404 error
    header("HTTP/1.x 404 Not Found");
    ?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL <?php echo $_SERVER["REQUEST_URI"]; ?> was not found on this server.</p>
<hr>
<?php echo $_SERVER["SERVER_SIGNATURE"]; ?>
</body></html>
<?
}
?>
